﻿using CMIS_WEB.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace CMIS_web_bootstrap.Controllers
{
    public class ContentController : Controller
    {
        public const string ip_server = "http://192.168.32.134:8080";
        public DotCMIS.Client.ISession ses = CMIS_Factory.LoginRepository("Admin", "training", ip_server + "/alfresco/service/cmis");
        // GET: Content
        public ActionResult Index()
        {
            List<Documento> lista_docs = new List<Documento>();
            lista_docs = CMIS_Factory.GetAllDocumentsByFolder("Admin", "training", ip_server+"/alfresco/service/cmis", "flacaixa");
            ViewData["folder list"] = lista_docs;
            return View();
        }

        [HttpPost]
        public ActionResult EliminarDocumento(String id_Doc)
        {
            string resultado = CMIS_Factory.borrar_documento("Admin", "training", ip_server + "/alfresco/service/cmis", id_Doc);
            if (resultado.Equals("OK")) {
                List<Documento> lista_docs = new List<Documento>();
                lista_docs = CMIS_Factory.GetAllDocumentsByFolder("Admin", "training", ip_server+ "/alfresco/service/cmis", "flacaixa");
                ViewData["folder list"] = lista_docs;
                return View("Index");
            }
            else
            {   
                return Content("<h1>No se ha eliminado el documento, se ha producido un error<\\h1><\\br><h3>"+resultado+"<\\h3>");
            }
        }


        [HttpPost]
        public ActionResult SubirDocumentacion(String nombre_presona, String apellidos, String numEmpleado, String dni, HttpPostedFileBase file)
        {
            //Primero lo subimos al directorio temporal del servidor
            string filePath = Path.Combine(Path.GetTempPath(), file.FileName);
            System.IO.File.WriteAllBytes(filePath, ReadData(file.InputStream));

            string log = string.Empty;

            Documento doc = new Documento();
            doc.nombre_presona=nombre_presona;
            doc.numEmpleado=numEmpleado;
            doc.apellidos=apellidos;
            doc.dni = dni;


            log = CMIS_Factory.crearDocumento(ses,"flacaixa", doc, filePath);


            //Una vez que lo tenemos en el directorio temportal, lo subimos a alfresco
            

            return Json(log);
        }

        private byte[] ReadData(Stream stream)
        {
            byte[] buffer = new byte[16 * 1024];

            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }

                return ms.ToArray();
            }
        }

        // GET: Content/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Content/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Content/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Content/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: Content/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Content/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Content/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
